package org.yaac.client.widget.stat;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import com.google.gwt.dom.client.Style;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlowPanel;

/**
 * @author Max Zhu (thebbsky@gmail.com)
 *
 */
public class CountSizeMultiFilterPanel extends FlowPanel {

	public static interface OnChangeHandler {
		void onChange(Set<CountSizeFilterChoice> vals);
	}
	
	private CheckBox countCb;
	
	private CheckBox sizeCb;

	private List<OnChangeHandler> handlers;
	
	/**
	 * 
	 */
	public CountSizeMultiFilterPanel() {
		handlers = new LinkedList<OnChangeHandler>();
		
		// count
		countCb = new CheckBox("Count");	// TODO : i18n
		countCb.getElement().getStyle().setFloat(Style.Float.LEFT);
		countCb.setValue(true);	// default select count
		countCb.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
			@Override
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				onChange();
			}
		});
		this.add(countCb);
		
		// size
		sizeCb = new CheckBox("Size");	// TODO : i18n
		sizeCb.setValue(true);
		sizeCb.addValueChangeHandler(new ValueChangeHandler<Boolean>() {

			@Override
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				onChange();
			}
		});
		this.add(sizeCb);
	}
	
	/**
	 * @return
	 */
	public Set<CountSizeFilterChoice> selectedValues() {
		Set<CountSizeFilterChoice> result = new HashSet<CountSizeFilterChoice>();
		
		if (countCb.getValue()) {
			result.add(CountSizeFilterChoice.COUNT);
		} 
		
		if (sizeCb.getValue()) {
			result.add(CountSizeFilterChoice.SIZE);
		}
		
		return result;
	}
	
	public void addOnChangeHandler(OnChangeHandler handler) {
		this.handlers.add(handler);
	}
	
	private void onChange() {
		for (OnChangeHandler handler : handlers) {
			handler.onChange(selectedValues());
		}
	}
}
